#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./paas/esb/apps/manager/channel/api_views.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
from django.views.generic import View
from django.conf import settings
from esb.bkcore.models import ESBChannel
from esb.common.django_utils import JsonResponse
from esb.common.decorators import is_user_super
DEFAULT_HOST = 'http://paas.bking.com'

class DeletedChannelView(View):
    """Deleted channel view"""

    @is_user_super
    def post(self, request):
        channel_ids = request.POST.get('channel_ids')
        channel_ids = channel_ids.split(',') if channel_ids else []
        objs = ESBChannel.objects.filter(id__in=channel_ids)
        affected_rows = objs.count()
        objs.delete()
        return JsonResponse({'affected_rows': affected_rows,'error_message': None})


class ChannelListSearchView(View):
    """根据过滤条件，筛选数据"""

    @is_user_super
    def get(self, request):
        system_name = request.GET.get('system_name')
        channel_path = request.GET.get('channel_path')
        channel_name = request.GET.get('channel_name')
        channels = ESBChannel.objects.all()
        if system_name:
            channels = channels.filter(component_system__name=system_name)
        if channel_name:
            channels = channels.filter(name__contains=channel_name)
        if channel_path:
            channels = channels.filter(path__contains=channel_path)
        try:
            host = settings.PAAS_DOMAIN
        except:
            host = DEFAULT_HOST

        if not host.startswith('http://'):
            host = 'http://%s' % host
        channels = [ {'id': channel.id,'name': channel.name,'label': u'[%s] %s' % (channel.component_system.name, channel.name),'path': channel.path,'method': channel.method,'component_codename': channel.component_codename,'is_active': channel.is_active,'created_time': channel.created_time.strftime('%Y-%m-%d'),'timeout_time': channel.timeout_time,'host': '%s/api/c/compapi' % host} for channel in channels.order_by('-is_active', 'component_system__name', 'path')
                   ]
        return JsonResponse({'data': channels})
